package br.ufrj.dcc.dojo.jtrace;

import java.io.IOException;
import java.util.List;

import org.jtrace.Scene;
import org.jtrace.Tracer;
import org.jtrace.ViewPlane;
import org.jtrace.shader.Shader;
import org.jtrace.swing.TracerPanel;

import br.ufrj.dcc.dojo.jtrace.texture.CylinderTextureShader;
import br.ufrj.dcc.dojo.jtrace.texture.SphereTextureShader;
import br.ufrj.dcc.dojo.jtrace.texture.TextureMappingEnum;

public abstract class TracerComponents {

	private Scene scene;
	private Tracer tracer;
	private ViewPlane viewPlane;
	private TracerPanel tracerPanel;
	private Shader textureShader;
	private int hres;
	private int vres;
	
	public TracerComponents(int hres, int vres) {
		this.hres = hres;
		this.vres = vres;
	}

	protected abstract void createScene(Scene scene) throws IOException;
	protected abstract void createTracer(Tracer tracer); 

	public void buildComponents() throws IOException {
		createViewPlane();
		createScene(scene = new Scene());
		createTracer(tracer = new Tracer()); 
		createTracerPanel();
	}
	
	private void createTracerPanel() {
		tracerPanel = new TracerPanel(tracer, scene, viewPlane, hres, vres);
	}
	
	private void createViewPlane() {
		viewPlane = new ViewPlane(hres, vres);
	}
	
	public void setTexture(String filename, TextureMappingEnum type) throws Exception {
		List<Shader> shaders = getTracer().getShaders();
		shaders.remove(textureShader);
		if (TextureMappingEnum.SPHERICAL == type) {
			textureShader = new SphereTextureShader(filename);	
		} else if (TextureMappingEnum.CYLINDRICAL == type) {
			textureShader = new CylinderTextureShader(filename);
		} else {
			throw new IllegalStateException("TextureMappingEnum inválido.");
		}
		shaders.add(textureShader);
	}
	
	public TracerPanel getTracerPanel() {
		return tracerPanel;
	}
	
	public Scene getScene() {
		return scene;
	}

	public Tracer getTracer() {
		return tracer;
	}
	
	public ViewPlane getViewPlane() {
		return viewPlane;
	}
}